<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/1/3
 * Time: 16:09
 */
namespace app\index\service;
use think\Db;
use Wechat\Lib\Tools;
use Wechat\Lib\Common;      //微信公共类
class FansService{
    /**
     * @Author lignyun
     * @Desc    初次进入门店领取门店的关注优惠券
     * @param $uid          会员id
     * @param $store_id     门店id
     */
    public function getStoreAttentionCoupon($uid,$store_id){
        //门店的关注券列表
        $attention_coupon = Db::name('store_coupon')->where(['store_id'=>$store_id,'type'=>1,'status'=>1])->select();
        //门店发布并且关注券有效
        if(!empty($attention_coupon)){
            $num = Db::name('user_ycard')->where(['uid'=>$uid,'store_id'=>$store_id,'type'=>1])->count();
            if($num == 0){         //没有领取该门店的关注券
                foreach($attention_coupon as $k => $v){
                    $data = array(
                        'uid'=>$uid,
                        'store_id'=>$store_id,
                        'coupon_id'=>$v['id'],
                        'type'=>1,      //门店关注券类型
                        'morethan'=>$v['morethan'],
                        'reduce'=>$v['reduce'],
                        'addtime'=>time(),
                        'is_used'=>0        //未使用
                    );
                    //插入会员关注券记录
                    Db::name('user_ycard')->insert($data);
                }
            }
        }
    }
    /**
     * 微信分享 -- 获取参数
     * @param  [type] $appid     公众号appid
     * @param  [type] $appsecret 公众号appsecret
     * @param  [type] $sys
     * @param  [type] $db        [description]
     * @return [type]            [description]
     */
    public function wxShare() {
        $config = [
            'token' => sysconf('wechat_token'),
            'appid' => sysconf('wechat_appid'),
            'appsecret' => sysconf('wechat_appsecret'),
            'encodingaeskey' => sysconf('wechat_encodingaeskey'),
        ];
        $common = new Common($config);
        $jsapiTicket = $this->getJsApiTicket($common);
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $timestamp = time();
        $nonceStr = Tools::createNoncestr(16);       //获取16位随机字符串
        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
        $signature = sha1($string);
        $signPackage = array(
            "appId"     => $common->appid,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string
        );
        return $signPackage;
    }
    /**
     * @Author lignyun
     * @Desc    获取参数
     * @param   $common   微信公共类实例
     * @return
     */
    private function getJsApiTicket($common) {
        $accessToken = $common->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
        $res = json_decode(Tools::httpGet($url),true);
        $ticket = $res['ticket'];
        return $ticket;
    }

}